import sys
parent_dir = "../pygl"
sys.path.append(parent_dir)
import ctypes
import OpenGL.GL as gl
import glm
import imgui
import numpy as np
import pygl
from common import App, AxesHelper, Cube

class CameraApp(App):
    # 初始化着色器，vao vbo等
    def setupVertex(self) -> None:
        gl.glEnable(gl.GL_DEPTH_TEST)
        self.background_color = glm.vec4(0.3, 0.3, 0.3, 1.0)
        self.cube = Cube(0.4, "textures/cratetex.png")
        self.cube_axes = AxesHelper(0.6, 5.0)
        self.world_axes = AxesHelper(0.9, 2.0)
        self.imgui_window_width = 250
        self.imgui_window_height = 450
        self.initCamera()

    def initCamera(self) ->None:
        self.camera_position = glm.vec3(0.0, 0.0, 3.0)
        self.camera_target = glm.vec3(0.0, 0.0, 0.0)
        self.camera_fov = 45.0
        self.camera_aspect = float(self.width)/self.height
        self.camera_near = 1.0
        self.camera_far = 6.0
        
    # 配置imgui界面元素
    def showImGui(self)->None:
        # 增加ImGui窗口的透明度
        imgui_style = imgui.get_style()
        color = imgui_style.colors[imgui.COLOR_WINDOW_BACKGROUND]
        imgui_style.colors[imgui.COLOR_WINDOW_BACKGROUND] = imgui.Vec4(color.x, 
                                                    color.y, color.z, 0.6)
        imgui.begin("照相机设置")
        _, self.cube.use_vertex_color = imgui.checkbox("使用顶点颜色", 
                                                self.cube.use_vertex_color)
        if imgui.button("重置"):
            self.initCamera()
        _, pos = imgui.slider_float3("位置", self.camera_position.x, 
                    self.camera_position.y, self.camera_position.z, 0.0, 10.0)
        self.camera_position = glm.vec3(pos)
        _, tg = imgui.slider_float3("目标", self.camera_target.x, 
                        self.camera_target.y, self.camera_target.z, -0.5, 0.5)
        self.camera_target = glm.vec3(tg)
        _, self.camera_fov = imgui.slider_float("视野", self.camera_fov,5, 170)
        _, self.camera_near = imgui.slider_float("近面", self.camera_near,1.0, 10.0)
        _, self.camera_far = imgui.slider_float("远面", self.camera_far,1.0, 10.0)
        imgui.text("视图矩阵")
        imgui.begin_table("view data", 4)
        for i in range(4):
            imgui.table_next_row()
            for j in range(4):
                imgui.table_next_column()
                imgui.text(str("{:.3f}").format(self.view[j, i]))
        imgui.end_table()
        imgui.text("投影矩阵")
        imgui.begin_table("projection data", 4)
        for i in range(4):
            imgui.table_next_row()
            for j in range(4):
                imgui.table_next_column()
                imgui.text(str("{:.3f}").format(self.projection[j, i]))
        imgui.end_table()
        imgui.end()       

    # 渲染图形
    def render(self) -> None:
        gl.glClearBufferfv(gl.GL_COLOR, 0, self.background_color.to_tuple())
        gl.glClearBufferfv(gl.GL_DEPTH, 0, 1.0)
        self.view = glm.lookAt(self.camera_position, self.camera_target, 
                               glm.vec3(0.0, 1.0, 0.0))
        self.projection = glm.perspective(glm.radians(self.camera_fov), 
                            self.camera_aspect, self.camera_near, self.camera_far)
        self.cube.updateModelMatrix()
        self.cube.render(self.view,self.projection)
        self.cube_axes.model_matrix = self.cube.model_matrix
        self.cube_axes.render(self.view, self.projection)
        self.world_axes.render(self.view, self.projection)

    # 释放资源
    def cleanup(self) -> None:
        self.cube.delete()
        self.cube_axes.delete()
        self.world_axes.delete()

if __name__ == "__main__":
    app = CameraApp(100, 100, 800, 800, "你好，照相机App！")
    app.run()

